package com.example.qxfw.hbqx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.qxfw.common.entity.qx.*;
import com.example.qxfw.common.entity.xt.YbzzConfig;
import com.example.qxfw.common.entity.zy.AirHazeWarn;
import com.example.qxfw.hbqx.mapper.*;
import com.example.qxfw.hbqx.service.QxybcpService;
import com.example.qxfw.qxzs.mapper.SpecialSrvStationMapper;
import com.example.qxfw.ybzz.mapper.TownshipForecastMapper;
import com.example.qxfw.ybzz.mapper.TownshipStationMapper;
import com.example.qxfw.ybzz.mapper.YbzzConfigMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
@Service
public class QxybcpServiceImpl implements QxybcpService{
    @Autowired
    private YbzzConfigMapper ybzzConfigMapper;
    @Autowired
    private TownshipStationMapper townshipStationMapper;
    @Autowired
    private SpecialSrvStationMapper specialSrvStationMapper;
    @Autowired
    private HighElementsMapper highElementsMapper;
    @Autowired
    private GroundElementsMapper groundElementsMapper;
    @Autowired
    private TempInversionybMapper tempInversionybMapper;
    @Autowired
    private AirHazeWarnMapper airHazeWarnMapper;
    @Autowired
    private HazeVisibilityForecastMapper hazeVisibilityForecastMapper;
    @Autowired
    private TownshipForecasttempMapper townshipForecasttempMapper;
    @Autowired
    private MixingHeightMapper mixingHeightMapper;
    @Autowired
    private TempInversionMapper tempInversionMapper;
    @Autowired
    private TownshipForecastMapper townshipForecastMapper;




    /**
     * 通过要素类型查询预报制作参数
     * @param elementtype 要素类型
     * @return
     */
    @Override
    public List<YbzzConfig> selectByElementTypeOfYbzzConfig(String elementtype) {
        return ybzzConfigMapper.selectList(new LambdaQueryWrapper<YbzzConfig>()
                .eq(StringUtils.isNotBlank(elementtype),YbzzConfig::getElementtype,elementtype));
    }

    /**
     * 通过站点名称查询所有乡镇预报站点
     * @param stationname 站点名称
     * @return
     */
    @Override
    public List<TownshipStation> selectByStationNameOfTownshipStation(String stationname) {
        return townshipStationMapper.selectList(new LambdaQueryWrapper<TownshipStation>()
                .like(StringUtils.isNotBlank(stationname),TownshipStation::getStationname,stationname));
    }

    /**
     * 根据站点类型查询所有站点名称和站点id
     * @param flag 站点类型
     * @return
     */
    @Override
    public List<SpecialSrvStation> selectByFlagOfSpecialSrvStation(String flag) {
        return specialSrvStationMapper.selectList2(flag);
    }

    /**
     *根据观测时间查询高空预报
     * @param observtime 观测时间
     * @return
     */
    @Override
    public List<HighElements> selectByObservTimeOfHighElements(Date observtime) {
        return highElementsMapper.selectByObservTimeOfHighElements(observtime);
    }

    /**
     * 根据观测时间查询地面要素温度，站点名称和id
     * @param observtime
     * @return
     */
    @Override
    public List<GroundElements> selectByObservtimeOfGroundElements(Date observtime) {
        return groundElementsMapper.selectByObservtimeOfGroundElements(observtime);
    }

    /**
     *根据预报时间时次类型查询逆温预报
     * @param datechar 预报时间
     * @param timechar 预报时次
     * @param fcsttype 预报类型
     * @return
     */
    @Override
    public List<TempInversionyb> selectByDatecharAndTimecharAndFcsttypeOfTempInversionyb(Date datechar, Date timechar, String fcsttype) {
        return tempInversionybMapper.selectByDatecharAndTimecharAndFcsttypeOfTempInversionyb(datechar, timechar, fcsttype);
    }

    /**
     *根据日期段查询霾预报产品
     * @param datecharStart 起始日期
     * @param datecharEnd 结束日期
     * @return
     */
    @Override
    public List<AirHazeWarn> selectByDateCharOfAirHazeWarn(Date datecharStart, Date datecharEnd) {
        return airHazeWarnMapper.selectByDateCharOfAirHazeWarn(datecharStart, datecharEnd);
    }

    /**
     *根据日期和站点id删除能见度预报
     * @param datechar 日期
     * @param stationid 站点id
     * @return
     */
    @Override
    public int deleteByDateCharAndStationIdOfHazeVisibilityForecast(Date datechar, String stationid) {
        return hazeVisibilityForecastMapper.deleteByDateCharAndStationIdOfHazeVisibilityForecast(datechar, stationid);
    }
    /**
     * 根据站点id，名称，日期，时效，能见度添加数据
     * @param stationid 站点id
     * @param stationname 站点名称
     * @param datechar 日期
     * @param ntimes 时效
     * @param hazevisibility 能见度范围
     * @return
     */
    @Override
    public int insertByStationIDAndStationNameAndDateCharAndNTimesAndHazeVisibilityOfHazeVisibilityForecast(String stationid, String stationname, Date datechar, Date ntimes, Double hazevisibility) {
        return hazeVisibilityForecastMapper.insertByStationIDAndStationNameAndDateCharAndNTimesAndHazeVisibilityOfHazeVisibilityForecast(stationid, stationname, datechar, ntimes, hazevisibility);
    }

    /**
     * 根据时间段和站点查询能见度报表时期，时效，能见度
     * @param stationid 站点id
     * @param datecharStart 起始时间
     * @param datecharEnd 终止时间
     * @return
     */
    @Override
    public List<HazeVisibilityForecast> selectByIdAndTimeframeOfHVF(String stationid, Date datecharStart, Date datecharEnd) {
        return hazeVisibilityForecastMapper.selectByIdAndTimeframeOfHVF(stationid, datecharStart, datecharEnd);
    }

    /**
     * 通过站点id和日期查询能见度预报
     * @param stationid 站点id
     * @param datechar 日期
     * @return
     */
    @Override
    public List<HazeVisibilityForecast> selectByIdAndTimeOfHazeVisibilityForecast(String stationid, Date datechar) {
        return hazeVisibilityForecastMapper.selectByIdAndTimeOfHazeVisibilityForecast(stationid, datechar);
    }

    /**
     * 根据日期时效时次站点id查询乡镇预报临时表
     * @param datechar 日期
     * @param ntimes 时效
     * @param timechar 时次
     * @param stationid 站点编号
     * @return
     */
    @Override
    public List<TownShipForecastTemp> selectByIdNtimesDatecharTimechar(Date datechar, Integer ntimes, String timechar, String stationid) {
        return townshipForecasttempMapper.selectByIdNtimesDatecharTimechar(datechar, ntimes, timechar, stationid);
    }

    /**
     * 根据日期类型和时次查询混合层高度
     * @param datechar 日期
     * @param forecasttype 类型
     * @param ntimes 时次
     * @return
     */
    @Override
    public List<MixingHeight> selectByDateAndTypeAndTimesOfMixingHeight(Date datechar, Integer forecasttype, Integer ntimes) {
        return mixingHeightMapper.selectList(new LambdaQueryWrapper<MixingHeight>()
        .eq(datechar != null, MixingHeight::getDatechar,datechar)
        .eq(forecasttype != 0,MixingHeight::getForecasttype,forecasttype)
        .eq(ntimes != 0,MixingHeight::getNtimes,ntimes));
    }

    /**
     * 根据日期和时次查询逆温实况表
     * @param datechar 日期
     * @param timechar 时次
     * @return
     */
    @Override
    public List<TempInversion> selectByDateAndTimeOfTempInversion(Date datechar, String timechar) {
        return tempInversionMapper.selectList(new LambdaQueryWrapper<TempInversion>()
        .eq(datechar != null,TempInversion::getDatechar,datechar)
        .eq(StringUtils.isNotBlank(timechar),TempInversion::getTimechar,timechar));
    }

    /**
     * 根据时间段获取能见度预报信息
     * @param datecharStart 开始时间
     * @param datecharEnd 结束时间
     * @return
     */
    @Override
    public List<HazeVisibilityForecast> selectByTimePeriodOfHazeVisibilityForecast(Date datecharStart, Date datecharEnd) {
        return hazeVisibilityForecastMapper.selectList(new LambdaQueryWrapper<HazeVisibilityForecast>()
        .between(HazeVisibilityForecast::getDatechar,datecharStart,datecharEnd));
    }

    /**
     * 根据时间点获取能见度预报信息
     * @param datechar 时间点
     * @return
     */
    @Override
    public List<HazeVisibilityForecast> selectByTimeOfHazeVisibilityForecast(Date datechar) {
        return hazeVisibilityForecastMapper.selectList(new LambdaQueryWrapper<HazeVisibilityForecast>()
                .eq(datechar != null,HazeVisibilityForecast::getDatechar,datechar));
    }

    /**
     * 获取所有乡镇预报主表信息
     * @return
     */
    @Override
    public List<TownshipForecast> getAllOfTownshipForecast() {
        return townshipForecastMapper.selectList(null);
    }

    /**
     * 根据日期时次时效站点名字获取气象要素信息
     * @param datechar 日期
     * @param timechar 时次
     * @param ntimes 时效
     * @param stationname 站点名字
     * @return
     */
    @Override
    public List<TownshipForecast> selectByDateTimecharNtimesNameOfTownshipForecast(
            Date datechar, String timechar, Integer ntimes, String stationname) {
        return townshipForecastMapper.selectList(new LambdaQueryWrapper<TownshipForecast>()
        .eq(datechar != null, TownshipForecast::getDatechar, datechar)
        .eq(StringUtils.isNotBlank(timechar), TownshipForecast::getTimechar, timechar)
        .eq(ntimes != 0, TownshipForecast::getNtimes, ntimes)
        .eq(StringUtils.isNotBlank(stationname), TownshipForecast::getStationname, stationname));
    }


}
